package com.gsq.zjcgj.base;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gy.mbaselibrary.fragment.LazyFragment;
import com.gy.mbaselibrary.inters.IKeyBoardVisibleListener;
import com.gy.mbaselibrary.utils.ToastUtil;

import java.util.List;
import java.util.Map;

/**
 * 创建者：高业
 * 时间：2018/2/6
 * 类描述：项目fragment的基类，项目fragment里的公共方法在这里里面
 * 修改人：
 * 修改时间：2018/2/6
 * 修改备注：
 */

public abstract class ProjectBaseFragment extends LazyFragment {
    protected boolean isVisiableForLast = false;
    protected int currentPage;
    protected int pageSize;
    private FragmentManager fragmentManager;
    private Fragment currentFragment;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        //user = UserUtils.getUser(getCurrentContext());
        super.onCreate(savedInstanceState);
    }

    public void addOnSoftKeyBoardVisibleListener(Activity activity, final IKeyBoardVisibleListener listener) {
        final View decorView = activity.getWindow().getDecorView();
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                decorView.getWindowVisibleDisplayFrame(rect);
                //计算出可见屏幕的高度
                int displayHight = rect.bottom - rect.top;
                //获得屏幕整体的高度
                int hight = decorView.getHeight();
                //获得键盘高度
                int keyboardHeight = hight-displayHight;
                boolean visible = (double) displayHight / hight < 0.8;
                if(visible != isVisiableForLast){
                    listener.onSoftKeyBoardVisible(visible,keyboardHeight );
                }
                isVisiableForLast = visible;
            }
        });
    }


    //隐藏diolog
    public void hideNetDialog(){
        if (getActivity() instanceof ProjectBaseActivity){
            ((ProjectBaseActivity)getActivity()).hideNetDialog();
        }
    }


    //显示dialog
    public void showNetDialog(){
        if (getActivity() instanceof ProjectBaseActivity){
            ((ProjectBaseActivity)getActivity()).showNetDialog();
        }
    }

    //显示dialog
    public void showNetDialog(String str){
        if (getActivity() instanceof ProjectBaseActivity){
            ((ProjectBaseActivity)getActivity()).showNetDialog(str);
        }
    }

    @Override
    public <T> void success(int requestCode, String url, T baseBean, String response, Object tag, Map<String, Object> tagMap, Map<String, String> params, Map<String, String> headParams) {

    }

    @Override
    public void fail(int requestCode, String url, Exception e, Object tag, Map<String, Object> tagObj, Map<String, String> params, Map<String, String> headParams) {

    }


    //切换fragment
    protected void switchContent(Fragment to, int frId) {
        if (fragmentManager == null) {
            fragmentManager = getChildFragmentManager();
        }
        if (to != null && to != currentFragment) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (currentFragment != null) {
                if (!to.isAdded()) {    // 先判断是否被add过
                    transaction.hide(currentFragment).add(frId, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
                } else {
                    transaction.hide(currentFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个
                }
            } else {
                transaction.add(frId, to).commit();
            }
            currentFragment = to;
        }
    }

    @Override
    public void parseNetworkResponse(int requestCode, String url, List<String> list) {

    }

}
